<?php
namespace Home\Controller;
use Think\Controller;
class OrdersController extends MerchantController {
	
public function _initialize(){
	parent::_initialize();	
 	$this->model="orders"; //表名
	$this->model_view="OrdersView";//视图名
 	$this->key="order_id";//排序键		 
 	$this->title_index=L("transactions");//选项卡标题	
}

public function index(){
	
	$mist_arr=array();
	$accute_arr=array('order_id'=>I("order_id"),"mh_oid"=>I("mh_oid"),"card_type"=>I('card_type'),"order_status"=>I("order_status"),
	"order_siteid"=>I("order_siteid"),"bill_email"=>I("bill_email"),'bill_firstname'=>I("bill_firstname"),'bill_lastname'=>I("bill_lastname"),
	"bill_phone"=>I("bill_phone"));
	$cookie_arr=array();
	
	$con=searchCon($mist_arr,$accute_arr,$cookie_arr);
	$con['order_mid']=$this->merchant_info['mh_id'];
	if(I('order_time_start') && I('order_time_end')){
		$con['order_time']=array("between",array(I('order_time_start'),I('order_time_end')));
	}	

	//生成CSV
	if(I('create_csv')){
		$count=D($this->model_view)->where($con)->count();
		//超过200000条记录不予下载
		if($count>200000){
			$this->assign("download_flag",0);
		}else{
			$sql=D($this->model_view)->fetchSql(true)->where($con)->order($this->key." desc")->select();
			parent::createCsv($sql,C('ORDER_CSV'),"orders_".$this->merchant_info['mh_id']);
			$this->assign("download_flag",1);
		}
	}
	
	parent::home($con);
}

public function details(){
	$con['order_mid']=$this->merchant_info['mh_id'];
	$con['order_id']=I("path.2");
	//订单历史
	$oop=M('orderhistory');
	$list=$oop->where(array('oh_oid'=>I("path.2")))->field(true)->order("oh_id desc")->select();
	//dump($list);
	$this->assign("history",$list);
	parent::details($con);	
}

/**
*退款方法，调用退款接口
*/
public function processRefund(){
	$oop=D("OrdersView");
	$con['order_mid']=$this->merchant_info["mh_id"];
	$con['order_id']=I("order_id");
	$list=$oop->where($con)->find();
	$hash_info=hash("sha256",$list['order_mid'].$list['order_siteid'].$list['order_id'].I("refund_amount").$list['site_key']);
	A("Openapi/Orders")->refund($list['order_mid'],$list['order_siteid'],$list['order_id'],I("refund_amount"),$hash_info);
}

/**
*取消退款
*/
public function cancelRefund(){
	$oop=D("OrdersView");
	$con['order_mid']=$this->merchant_info["mh_id"];
	$con['order_id']=I("order_id");
	$list=$oop->where($con)->find();
	$hash_info=hash("sha256",$list['order_mid'].$list['order_siteid'].$list['order_id'].$list['site_key']);
	A("Openapi/Orders")->cancelRefund($list['order_mid'],$list['order_siteid'],$list['order_id'],$hash_info);	
}

/**
*取消预授权
*/
public function void(){
	$oop=D("OrdersView");
	$con['order_mid']=$this->merchant_info["mh_id"];
	$con['order_id']=I("order_id");
	$list=$oop->where($con)->find();
	$hash_info=hash("sha256",$list['order_mid'].$list['order_siteid'].$list['order_id'].$list['site_key']);
	A("Openapi/Orders")->void($list['order_mid'],$list['order_siteid'],$list['order_id'],$hash_info);		
}


public function downloadCsv(){
	parent::downloadCsv("orders_".$this->merchant_info['mh_id'].".csv");
}

	
}

